/**开场动画 */
class ScreenStartAniamtion extends egret.DisplayObjectContainer {
    private touchOff = true
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        const miva = this

        miva.openBgm()

        miva.stage.addEventListener('OPEN_BGM_COMPONT', _ => {
            miva.createBg()
            miva.createText()
            miva.switchEvent()
        }, miva)
    }
    // 创建遮罩
    private openBgm() {
        const miva = this
        const container = new egret.Sprite()
        const btn = Utils.createBitmapByName(`btn_tip_mask_png`)
        const text = Utils.createText('请开启背景音乐', { x: 0, y: 0 }, container, false)

        container.addChild(btn)
        miva.addChild(container)
        const color = document.querySelector('#bgColor').innerHTML
        console.log(color)
        text.textColor = parseInt(color)
        text.strokeColor = 0xffffff
        btn.y = btn.x = 80
        Utils.anchorCenter(container)
        container.x = this.stage.stageWidth * .5
        container.y = miva.stage.stageHeight * .5

        Utils.dragDisplayObject({
            target: btn,
            container: container,
            that: miva,
            funObj: {
                startFun(params) {
                    Utils.superStopPropagation(params.evt)
                    miva.removeChild(container)
                    // 播放bgm
                    ParamsPool.bgmPlay()
                    miva.stage.dispatchEvent(new egret.Event('OPEN_BGM_COMPONT'))
                }
            }
        })
    }
    // 创建文字
    private createText() {
        const miva = this
        const png = Utils.createBitmapByName('wz_01_png')

        miva.addChild(png)

        png.y = png.x = 45

        miva.addEventListener('SWITCH_OPEN_TEXT', evt => {
            png.texture = RES.getRes(`wz_0${evt.data - 1}_png`)
        }, miva)
    }
    // 创建背景图
    private createBg() {
        const miva = this
        const bg_1 = Utils.createBitmapByName('start_01_png')
        const bg_2 = Utils.createBitmapByName('start_02_png')
        let num = 2
        let show = !0
        miva.addChild(bg_2)
        miva.addChild(bg_1)

        miva.addEventListener('SWITCH_OPEN_ANIMATION', _ => {
            if (!miva.touchOff) return console.log('wait')
            num++
            show = !show
            miva.touchOff = false
            if (num > 6) {
                miva.stage.dispatchEvent(new egret.Event('START'))
                return
            }
            if (!show) {
                egret.Tween.get(bg_1).to({ alpha: 0 }, 800).call(_ => {
                    miva.touchOff = true
                    bg_1.texture = RES.getRes(`start_0${num}_png`)
                })
            } else {
                egret.Tween.get(bg_1).to({ alpha: 1 }, 800).call(_ => {
                    miva.touchOff = true
                    bg_2.texture = RES.getRes(`start_0${num}_png`)
                })
            }

            miva.dispatchEventWith('SWITCH_OPEN_TEXT', false, num, false)
        }, miva)
    }
    // 给场景自身绑定切图事件
    private switchEvent() {
        const miva = this
        let count = 5
        const timer: egret.Timer = new egret.Timer(1000 * 5, count)
        timer.addEventListener(egret.TimerEvent.TIMER, _ => {
            count--
            miva.dispatchEvent(new egret.Event('SWITCH_OPEN_ANIMATION'))
        }, miva)
        timer.start()
        miva.touchEnabled = true
        miva.addEventListener(egret.TouchEvent.TOUCH_BEGIN, _ => {
            timer.repeatCount = count--
            timer.reset()
            timer.start()
            // // 播放bgm
            // ParamsPool.bgmPlay()
            miva.dispatchEvent(new egret.Event('SWITCH_OPEN_ANIMATION'))
        }, miva)

    }
}